package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Time;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;

    /**
     * 上传文件，读取文件内容，保存到数据库
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> stringList = POIUtils.readExcel(excelFile);

            //设置一个List<OrderSetting> 集合，把读取到的数据存储到这里，然后传给service
            List<OrderSetting> orderSettingList = new ArrayList<>();

            for (String[] strings : stringList) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new SimpleDateFormat("yyyy/MM/dd").parse(strings[0]));
                orderSetting.setNumber(Integer.parseInt(strings[1]));

                orderSettingList.add(orderSetting);
            }

            orderSettingService.editorderSettingList(orderSettingList);

            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
    }


    /**
     * 日历展示预约设置信息
     */
    @RequestMapping("/findByMonth")
    public Result findByMonth(String month){
        try {
            List<OrderSetting> orderSettingList = orderSettingService.findByMonth(month);

            //我们需要转换成前端需要的格式
            List<Map<String,Object>> mapList = new ArrayList<>();

            for (OrderSetting orderSetting : orderSettingList) {
                Map<String,Object> map = new HashMap<>();

                map.put("date",new SimpleDateFormat("dd").format(orderSetting.getOrderDate()));
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());

                //把Map添加到集合中
                mapList.add(map);
            }

            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,mapList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }

    }

    /**
     * 基于日历实现预约设置
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderSetting orderSetting){
        try {
            List<OrderSetting> orderSettingList = new ArrayList<>();

            orderSettingList.add(orderSetting);
            orderSettingService.editorderSettingList(orderSettingList);

            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }

    }


    /**
     * 保存定时清除时间信息
     * @param
     * @return
     */
    @RequestMapping("/DeleteTime")
    public Result DeleteTime(@RequestBody Time time){
        try {

            orderSettingService.DeleteTime(time);

            return new Result(true, MessageConstant.TIME_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.TIME_FAIL);
        }

    }

    /**
     * 时间回显
     * @return
     */
    @RequestMapping("/SetTime")
    public Result DeleteTime(){
        try {
            Time time = orderSettingService.findMonth();
            String Time=null;
            if (time.getMonth()==null){
                Time = "上次设置时间："+time.getDate()+"日";
            }else {
                Time = "上次设置时间："+time.getMonth()+"月"+time.getDate()+"日";
            }

            return new Result(true, MessageConstant.TIME_SUCCESS,Time);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.TIME_FAIL);
        }

    }


}
